package com.example.testdb1.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtils {

    private static final String signWithName = "token";
    private static final Long expiration = 60 * 1000L;


    /**
     * 生成jwt令牌
     *
     * @param map
     * @return
     */
    public static String createJwt(Map map) {

        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, signWithName)
                .setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + expiration))
                .compact();
    }

    /**
     * 解析JWT令牌
     *
     * @param jwt
     * @return
     */
    public static Claims resolveJwt(String jwt) {
        return Jwts.parser()
                .setSigningKey(signWithName)
                .parseClaimsJws(jwt)
                .getBody();
    }
}
